package com.miya.enums;

import com.miya.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.EnumSet;
import java.util.Set;

/**
 * 订单类型枚举
 *
 * @author Caixiaowei
 * @date 2022/02/11
 */
@Getter
@AllArgsConstructor
public enum OrderTypeEnum {

    /**
     * 秒杀
     */
    SPIKE(1, "秒杀"),

    /**
     * 团购
     */
    GROUP_BUY(2, "团购");

    private final Integer code;
    private final String desc;

    private static final Set<OrderTypeEnum> ALL = EnumSet.allOf(OrderTypeEnum.class);

    public static OrderTypeEnum getName(Integer code) {
        if (code == null) {
            return null;
        }

        return ALL.stream()
                .filter(o -> o.code.equals(code))
                .findAny().orElseThrow(() -> new BusinessException("订单类型枚举: " + code + "未匹配到相关值！"));
    }

    public boolean is(Integer code) {
        return getCode().equals(code);
    }
}
